package com.ai.platform.business.service.impl;

import com.ai.platform.business.entity.BusiReport;
import com.ai.platform.business.mapper.BusiDbInfoMapper;
import com.ai.platform.business.mapper.BusiReportMapper;
import com.ai.platform.business.service.IBusiReportService;
import com.ai.platform.common.enums.ReportChartTypeEnums;
import com.ai.platform.core.exception.BusinessException;
import com.ai.platform.model.dto.Input;
import com.ai.platform.model.dto.Output;
import com.ai.platform.report.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 *
 * @author yeyingsheng
 * @since 2018-06-01
 */
@Service
public class BusiReportServiceImpl extends ServiceImpl<BusiReportMapper, BusiReport> implements IBusiReportService {

    @Autowired
    private BusiDbInfoMapper dbInfoMapper;

    @Override
    public void reportList(Input input, Output output) throws BusinessException {
        int limit =Integer.parseInt(input.getValue("limit"));
        int pageNow = Integer.valueOf(input.getValue("page"));
        EntityWrapper<BusiReport> wrapper=new EntityWrapper<>();
        Page<BusiReport> page = new Page(pageNow,limit);
        page.setTotal(this.selectCount(wrapper));
        page=this.selectPage(page,wrapper);
        output.setCount(page.getTotal());
        List<BusiReport> list=page.getRecords();
        output.setData(list);
    }

    @Override
    public void reportListById(Input input, Output output) throws BusinessException {
        String reportId=input.getValue("reportId");
        BusiReport busiReport=this.selectOne(new EntityWrapper<BusiReport>().eq("report_id",reportId));
        output.setData(busiReport);
    }

    @Override
    public void reportChartListByReportId(Input input, Output output) throws BusinessException {
        String reportId=input.getValue("reportId");
        List<Map<String,Object>> list=this.baseMapper.queryReportCharts(reportId);
        output.setData(list);
    }

    @Override
    public void reportChartDataById(Input input, Output output) throws BusinessException {
        String chartId=input.getValue("chartId");
        Map<String,Object> map=this.baseMapper.reportChartDataById(chartId);
        if(!CollectionUtils.isEmpty(map)){
            String chartType= MapUtils.getString(map,"chartType");
            IDataHandle dataHandle=null;
            /**TABLE表格**/
            if(ReportChartTypeEnums.TABLE.getId().equals(chartType)){
                int limit =Integer.parseInt(input.getValue("limit"));
                int pageNow = Integer.valueOf(input.getValue("page"));
                dataHandle=new TableDataHandle(limit,pageNow,map);
            }
            /**折线图*/
            else if(ReportChartTypeEnums.LINE.getId().equals(chartType)){
                dataHandle=new LineDataHandle(map);

            }
            /**饼状图**/
            else if(ReportChartTypeEnums.PIE.getId().equals(chartType)){
                dataHandle=new PieDataHandle(map);

            }
            /**柱状图**/
            else if(ReportChartTypeEnums.BAR.getId().equals(chartType)){
                dataHandle=new BarDataHandle(map);

            }
            if(null!=dataHandle) {
                dataHandle.pullData(output);
            }
        }
    }
}
